//
//  GYSetPayPwdViewController.m
//  GoodLuck
//
//  Created by apple on 2018/6/23.
//  Copyright © 2018年 yunjiazhineng. All rights reserved.
//


#import "GYSetPayPwdViewController.h"
#import "GYNetWorkTools.h"
#import "GYUserSetViewController.h"

@interface GYSetPayPwdViewController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UIButton *subBtu;
@property (weak, nonatomic) IBOutlet UITextField *pwdTF;
@property (weak, nonatomic) IBOutlet UITextField *confirmPwdTF;

@end

@implementation GYSetPayPwdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
}

- (void)setupUI {
    self.navigationItem.title = @"设置密码";
    
}
- (IBAction)onClick:(UIButton *)sender {
    [self.view endEditing:YES];
    //判断密码长度是不是6个长度
    if (self.pwdTF.text.length != 6 || self.confirmPwdTF.text.length != 6) {
        [EasyTextView showText:@"请输入6位数密码!"];
        return;
    }
    
    if (![self.pwdTF.text isEqualToString:self.confirmPwdTF.text]) {
        [EasyTextView showText:@"您两次输入的密码不一样,请重新输入!"];
        return;
    }
    
    //提交设置密码
    [GYNetWorkTools requestPayPwd:self.pwdTF.text password1:self.confirmPwdTF.text success:^(NSDictionary *obj) {
        //NSLog(@"obj=======%@",obj);
        int code = [[obj objectForKey:@"code"] intValue];
        if (code == 200) {
            [EasyTextView showText:@"设置密码成功!"];
            for (UIViewController *viewController in self.navigationController.viewControllers) {
                if ([viewController isKindOfClass:[GYUserSetViewController class]]) {
                    [self.navigationController popToViewController:viewController animated:YES];
                    
                }
            }
        }else if (code == 400) {
            [EasyTextView showText:@"设置密码失败,请检查网络之后,重新设置!"];
        }
    } error:^(NSError *error) {
        [EasyTextView showText:@"设置密码失败,请检查网络之后,重新设置!"];
        NSLog(@"error======%@",error);
    }];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if (textField.text.length >= 6) {
        return NO;
    }
    return YES;
}
@end
